package calculator;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Panel;
import javax.swing.JLabel;
import java.awt.FlowLayout;

public class CalcDiag extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JLabel label;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CalcDiag dialog = new CalcDiag();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unused")
	private JButton createButton(int gridx, int gridy, final String value)
	{
		// this function could be used to create the buttons, but the WindowsBuilder does not like it
		JButton button = new JButton(value);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textField.setText(textField.getText() + value);
			}
		});
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.insets = new Insets(0, 0, 5, 5);
		gbc_button.gridx = gridx;
		gbc_button.gridy = gridy;
		return button;
	}
	
	/**
	 * Create the dialog.
	 */
	public CalcDiag() {
		setResizable(false);
		setTitle("Calculator");
		setBounds(100, 100, 439, 251);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.WEST);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{120, 120, 120, 63, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			textField = new JTextField();
			GridBagConstraints gbc_textField = new GridBagConstraints();
			gbc_textField.gridwidth = 4;
			gbc_textField.fill = GridBagConstraints.BOTH;
			gbc_textField.insets = new Insets(0, 0, 5, 0);
			gbc_textField.gridx = 0;
			gbc_textField.gridy = 0;
			contentPanel.add(textField, gbc_textField);
			textField.setColumns(10);
		}
		{
			JButton button = new JButton("(");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "(");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 0;
			gbc_button.gridy = 1;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton(")");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + ")");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 1;
			gbc_button.gridy = 1;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("7");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "7");
				}
			});
			{
				JButton btnClear = new JButton("Clear");
				btnClear.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						textField.setText("");
						label.setText("Ready");
					}
				});
				{
					JButton btnPow = new JButton("pow");
					btnPow.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							textField.setText(textField.getText() + "^");
						}
					});
					GridBagConstraints gbc_btnPow = new GridBagConstraints();
					gbc_btnPow.insets = new Insets(0, 0, 5, 5);
					gbc_btnPow.gridx = 2;
					gbc_btnPow.gridy = 1;
					contentPanel.add(btnPow, gbc_btnPow);
				}
				GridBagConstraints gbc_btnClear = new GridBagConstraints();
				gbc_btnClear.insets = new Insets(0, 0, 5, 0);
				gbc_btnClear.gridx = 3;
				gbc_btnClear.gridy = 1;
				contentPanel.add(btnClear, gbc_btnClear);
			}
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 0;
			gbc_button.gridy = 2;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("8");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "8");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 1;
			gbc_button.gridy = 2;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("9");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "9");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 2;
			gbc_button.gridy = 2;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("/");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "/");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 0);
			gbc_button.gridx = 3;
			gbc_button.gridy = 2;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("4");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "4");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 0;
			gbc_button.gridy = 3;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("5");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "5");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 1;
			gbc_button.gridy = 3;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("6");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "6");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 2;
			gbc_button.gridy = 3;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("*");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "*");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 0);
			gbc_button.gridx = 3;
			gbc_button.gridy = 3;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("1");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "1");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 0;
			gbc_button.gridy = 4;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("2");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "2");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 1;
			gbc_button.gridy = 4;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("3");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "3");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 5);
			gbc_button.gridx = 2;
			gbc_button.gridy = 4;
			contentPanel.add(button, gbc_button);
		}
		{
			JButton button = new JButton("-");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "-");
				}
			});
			GridBagConstraints gbc_button = new GridBagConstraints();
			gbc_button.insets = new Insets(0, 0, 5, 0);
			gbc_button.gridx = 3;
			gbc_button.gridy = 4;
			contentPanel.add(button, gbc_button);
		}
		{
			{
				JButton button_1 = new JButton(".");
				button_1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						textField.setText(textField.getText() + ".");
					}
				});
				GridBagConstraints gbc_button_1 = new GridBagConstraints();
				gbc_button_1.insets = new Insets(0, 0, 5, 5);
				gbc_button_1.gridx = 0;
				gbc_button_1.gridy = 5;
				contentPanel.add(button_1, gbc_button_1);
			}
		}
		{
			JButton button_2 = new JButton("0");
			button_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					textField.setText(textField.getText() + "0");
				}
			});
			GridBagConstraints gbc_button_2 = new GridBagConstraints();
			gbc_button_2.insets = new Insets(0, 0, 5, 5);
			gbc_button_2.gridx = 1;
			gbc_button_2.gridy = 5;
			contentPanel.add(button_2, gbc_button_2);
		}
		{
			JButton button_1 = new JButton("=");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
					IExpression ex = Parser.parse(textField.getText());
					textField.setText(ex.calculate().toString());
					label.setText("Ready");
					}
					catch (Exception ex)
					{
						label.setText(ex.getLocalizedMessage());
					}
				}
			});
			GridBagConstraints gbc_button_1 = new GridBagConstraints();
			gbc_button_1.insets = new Insets(0, 0, 5, 5);
			gbc_button_1.gridx = 2;
			gbc_button_1.gridy = 5;
			contentPanel.add(button_1, gbc_button_1);
		}
		JButton button = new JButton("+");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textField.setText(textField.getText() + "+");
			}
		});
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.insets = new Insets(0, 0, 5, 0);
		gbc_button.gridx = 3;
		gbc_button.gridy = 5;
		contentPanel.add(button, gbc_button);
		{
			Panel panel = new Panel();
			FlowLayout flowLayout = (FlowLayout) panel.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			getContentPane().add(panel, BorderLayout.SOUTH);
			{
				label = new JLabel("Ready");
				panel.add(label);
			}
		}
		pack();
	}

}
